package com.notes.algo.sx;

public class Solution5 {
	public static void main(String[] args) {
		java.util.Scanner sc = new java.util.Scanner(System.in);
		String se = sc.nextLine();
		String edgeStr = sc.next();
		String[] seArr = se.split(" ", -1);
		String start = seArr[0];
		String end = seArr[1];
		String path = minPath(start, end, edgeStr);
		System.out.println(path);
	}

	public static String minPath(String start, String end, String edgeStr) {

		String path = "";

		java.util.Set<String> nodeSet = new java.util.HashSet<String>();
		java.util.List<java.util.Map<String, String>> nodeEdgeList = new java.util.ArrayList<java.util.Map<String, String>>();
		java.util.List<java.util.Map<String, String>> pathToList = new java.util.ArrayList<java.util.Map<String, String>>();
		java.util.Map<String, Integer> nodePathMap = new java.util.HashMap<String, Integer>();
		java.util.Map<String, java.util.Map<String, String>> minPathMap = new java.util.HashMap<String, java.util.Map<String, String>>();

		String[] edgeArr = edgeStr.split(",", -1);
		for (int i = 0; i < edgeArr.length; i ++) {
			String curEdge = edgeArr[i];
			String[] curEdgeArr = curEdge.split("-", -1);
			nodeSet.add(curEdgeArr[0]);
			nodeSet.add(curEdgeArr[1]);
			java.util.Map<String, String> nodeEdge = new java.util.HashMap<String, String>();
			nodeEdge.put("start", curEdgeArr[0]);
			nodeEdge.put("end", curEdgeArr[1]);
			nodeEdge.put("len", "1");
			nodeEdgeList.add(nodeEdge);

			java.util.Map<String, String> oppNodeEdge = new java.util.HashMap<String, String>();
			oppNodeEdge.put("start", curEdgeArr[1]);
			oppNodeEdge.put("end", curEdgeArr[0]);
			oppNodeEdge.put("len", "1");
			nodeEdgeList.add(oppNodeEdge);
		}
		java.util.Map<String, String> startNode = new java.util.HashMap<String, String>();
		startNode.put("start", start);
		startNode.put("end", start);
		startNode.put("len", "0");
		pathToList.add(startNode);
		nodePathMap.put(start, 0);
		for (String node : nodeSet) {
			minPathMap.put(node, null);
		}

		while(pathToList.size() > 0) {
			java.util.Map<String, String> curPath = pathToList.remove(0);
			String endNodeName = curPath.get("end");
			Integer len = Integer.valueOf(curPath.get("len"));

			nodePathMap.put(endNodeName, len);
			minPathMap.put(endNodeName, curPath);

			for (int i = 0; i < nodeEdgeList.size(); i ++) {
				java.util.Map<String, String> curNodeEdge = nodeEdgeList.get(i);
				String curNodeEdgeStart = curNodeEdge.get("start");
				if (endNodeName.equals(curNodeEdgeStart)) {
					String curNodeEdgeEnd = curNodeEdge.get("end");
					Integer curNodeEdgeLen = Integer.valueOf(curNodeEdge.get("len"));
					Integer startTolen = nodePathMap.get(curNodeEdgeEnd);
					if (startTolen == null || len + curNodeEdgeLen < startTolen) {
						java.util.Map<String, String> pathTo = new java.util.HashMap<String, String>();
						pathTo.put("start", endNodeName);
						pathTo.put("end", curNodeEdgeEnd);
						pathTo.put("len", len + curNodeEdgeLen + "");
						pathToList.add(pathTo);
						nodePathMap.put(curNodeEdgeEnd, len + curNodeEdgeLen);
					}
				}
			}
		}

		if (nodePathMap.get(end) == null) {
			return "0";
		}

		java.util.Map<String, String> endNode = minPathMap.get(end);
		String preStart = endNode.get("start");
		String endNodeName = endNode.get("end");
		path = endNodeName;
		while (!preStart.equals(start)) {
			java.util.Map<String, String> curNode = minPathMap.get(preStart);
			String curNodeName = curNode.get("end");
			path += " " + curNodeName;
			preStart = curNode.get("start");
		}

		path += " " + start;

		return path;
	}
}
